/**
 *	Copyright (c) TeamFlamingo And gray 2004-2009
 *	www.me-live.com.cn / www.i-gray.cn
 *
 *	[ WARNING ]
 *		This is NOT a freeware!
 *		You MUST get author's authorization before using it!
 *	[ WARNING ]
 *
 *	Name : user-register.js / Development Code Evolve
 *	Created / Modify : 2010-2-22 / 2010-3-24
 */
var	weUserRegister	=	Class({
	//	[CH]	初始化信息
	init	:	function ( obj, parent )
	{
		this.Error	=
		[
			'<img src="./images/error.jpg" />',
			'<img src="./images/right.jpg" />',
			'<img src="./images/loading.gif" />'
		];

		this.Error[3]	=	'<b>输入有误</b>';
		this.Error[4]	=	'<b>已被占用</b>';
		this.Error[5]	=	'<b>未知错误</b>';
		this.Error[6]	=	'<b>验证码错误</b>';
		this.Error[7]	=	'信息输入有误</u>';

		this.obj	=	obj;
		this.value	=	MDK.String.trim( obj.value );
		this.span	=	$$( 'span', this.obj.parentNode )[0];

		this.parent	=	parent ? parent : false;
	},

	//	[CH]	验证登录动作
	checkReg	:	function ()
	{
		var	$M	=	this;
		//var	dhtml	=	new dthml()
		if( !window.auth['email'] )
		{
			$( 'register-email' ).focus();
			this.span.innerHTML	=	'<u>Email ' + this.Error[7];
			return	false;
		}
		else if( !window.auth['username'] )
		{
			$( 'register-username' ).focus();
			this.span.innerHTML	=	'<u>用户名 ' + this.Error[7];
			return	false;
		}
		else if( !window.auth['password'] )
		{
			$( 'register-password' ).focus();
			this.span.innerHTML	=	'<u>密码' + this.Error[7];
			return	false;
		}
		else if( !window.auth['authcode'] )
		{
			$( 'register-authcode' ).focus();
			this.span.innerHTML	=	'<u>验证码' + this.Error[7];
			return	false;
		}
		else
		{
			//alert( '注册?' );
			//	[CH]	所有的验证信息都正确的话,先用MD5将密码加密
			//	[CH]	防止信息提交时密码被窃听
			var	password	=	$( 'register-password' ).value	=	MDK.md5( MDK.String.trim( $( 'register-password' ).value ) );
			var	password2	=	$( 'register-password2' ).value	=	MDK.md5( MDK.String.trim( $( 'register-password2' ).value ) );
			var	email		=	MDK.String.trim( $( 'register-email' ).value );
			var	username	=	MDK.String.trim( $( 'register-username' ).value );
			var	authcode	=	MDK.String.trim( $( 'register-authcode' ).value );
			//alert( $( 'register-password' ).value );

			var	data	=	'email=' + email + '&username=' + username + '&password=' + password + '&password2=' + password2 + '&authcode=' + authcode + '&hash=' + $M.parent.widget.hash( 48 );

			//alert( $M.hash( 48 ) );
			var	doRegister	=	function ()
			{
				if( MDK.Ajax.xmlHttp.readyState == 4 && MDK.Ajax.xmlHttp.status == 200 )
				{
					var	code	=	MDK.Ajax.xmlHttp.responseText.substr( 0, 6 );	//	[CH]	获取返回信息码
					var	msg		=	MDK.Ajax.xmlHttp.responseText.substr( 7 );		//	[CH]	获取返回信息
					var	sec		=	5;

					var	times	=	function ()
					{
						//sec	=	is_undefined( sec ) ? 5 : sec;
						sec--;
						//var	timer	=	timer ? timer : $$( 'b', MDK.Ajax.xmlHttp.responseText )[0];
						if( sec != 0 )
							var	timerId	=	window.setInterval( times, 1000 );
						else
						{
							clearInterval( timerId );
							location.reload( true );
						}
					}

					//alert( code );
					if( code == 'Err[4]' )
					{
						//var	sec	=	is_undefined( sec ) ? 5 : sec;
						msg	=	'<a onclick="location.reload(true)">' + msg + '</a>';
						$M.parent.dhtml.loading( msg );
						times();
					}
					else
					{
						$M.parent.dhtml.loading( msg );
					}
				}
				else
				{
					//	[CH]	禁止所有的编辑修改动作
					$( 'register-email' ).unselectable	=	'on';
					$( 'register-username' ).unselectable	=	'on';
					$( 'register-password' ).unselectable	=	'on';
					$( 'register-password2' ).unselectable	=	'on';
					$( 'register-authcode' ).unselectable	=	'on';
					$( 'do-register' ).unselectable			=	'on';

					//	[CH]	将窗口隐藏掉
					$( 'me-register-window' ).style.display	=	'none';

					$M.parent.dhtml.loading( '正在提交注册信息，请稍等...' );
				}
			}

			MDK.Ajax.init( 'post', './u/register.php', doRegister, data );
		}
		return	false;
	},

	//	[CH]	检测电子邮件
	checkEmail	:	function ()
	{
		var	$M	=	this;
		if( this.parent.widget.isEmail( this.value ) )
		{
			var	value	=	'v=' + this.value + '&hash=' + $M.parent.widget.hash( 12 );
			var	reqEmail	=	function ()
			{
				if( MDK.Ajax.xmlHttp.readyState == 4 && MDK.Ajax.xmlHttp.status == 200 )
				{
					if( MDK.Ajax.xmlHttp.responseText == 'Err[1]' )
						$M.span.innerHTML	=	$M.Error[0] + $M.Error[4];
					else if( MDK.Ajax.xmlHttp.responseText == 'Err[2]' )
						$M.span.innerHTML	=	$M.Error[0] + $M.Error[3];
					else if( MDK.Ajax.xmlHttp.responseText == 'Err[3]' )
						$M.span.innerHTML	=	$M.Error[5];
					else
					{
						$M.span.innerHTML	=	$M.Error[1];
						window.auth['email']	=	true;
					}
				}
				else
				{
					$M.span.innerHTML	=	$M.Error[2];
				}
			}

			MDK.Ajax.init( 'post', './u/register.php', reqEmail, value );
		}
		else
		{
			this.span.innerHTML	=	this.Error[0] + this.Error[3];
			return	false;
		}
	},

	checkUsername	:	function ()
	{
		var	$M	=	this;
		if( this.parent.widget.isUsername( this.value ) )
		{
			var	value	=	'v=' + this.value + '&hash=' + $M.parent.widget.hash( 24 );
			var	reqUsername	=	function ()
			{
				if( MDK.Ajax.xmlHttp.readyState == 4 && MDK.Ajax.xmlHttp.status == 200 )
				{
					if( MDK.Ajax.xmlHttp.responseText == 'Err[1]' )
						$M.span.innerHTML	=	$M.Error[0] + $M.Error[4];
					else if( MDK.Ajax.xmlHttp.responseText == 'Err[2]' )
						$M.span.innerHTML	=	$M.Error[0] + $M.Error[3];
					else if( MDK.Ajax.xmlHttp.responseText == 'Err[3]' )
						$M.span.innerHTML	=	$M.Error[5];
					else
					{
						$M.span.innerHTML	=	$M.Error[1];
						window.auth['username']	=	true;
					}
				}
				else
				{
					$M.span.innerHTML	=	$M.Error[2];
				}
			}

			MDK.Ajax.init( 'post', './u/register.php', reqUsername, value );
		}
		else
		{
			this.span.innerHTML	=	this.Error[0] + this.Error[3];
			return	false;
		}
	},

	checkPassword	:	function ()
	{
		var	obj1	=	$( 'register-password' );
		var	obj2	=	$( 'register-password2' );

		var	value1	=	MDK.String.trim( $( 'register-password' ).value );
		var	value2	=	MDK.String.trim( $( 'register-password2' ).value );

		if( this.obj == obj1 )	//	如果当前是密码对话框
		{
			if( ( value2 != '' || value2 != null ) && value1 == value2 )
			{
				this.span.innerHTML	=	this.Error[1];
				$$( 'span', obj2.parentNode )[0].innerHTML	=	this.Error[1];
				window.auth['password']	=	true;
			}
			if( this.value.length <= 4 || this.value.length >= 20 )
				this.span.innerHTML	=	this.Error[0] + this.Error[3]
		}

		if( this.obj == obj2 )
		{
			if( this.value.length >= 4 && this.value.length <= 20 && value1 == value2 )
			{
				this.span.innerHTML	=	this.Error[1];
				$$( 'span', obj1.parentNode )[0].innerHTML	=	this.Error[1];
				window.auth['password']	=	true;
			}
			else
			{
				this.span.innerHTML	=	this.Error[0] + this.Error[3];
				return	false;
			}
		}
	},

	checkAuthcode	:	function ()
	{
		var	$M	=	this;
		if( this.parent.widget.isNumber( this.value ) )
		{
			var	value	=	'v=' + this.value + '&hash=' + $M.parent.widget.hash( 36 );
			var	reqAuthcode	=	function ()
			{
				if( MDK.Ajax.xmlHttp.readyState == 4 && MDK.Ajax.xmlHttp.status == 200 )
				{
					if( MDK.Ajax.xmlHttp.responseText == 'Err[1]' )
						$M.span.innerHTML	=	$M.Error[0] + $M.Error[6];
					else if( MDK.Ajax.xmlHttp.responseText == 'Err[2]' )
						$M.span.innerHTML	=	$M.Error[0] + $M.Error[3];
					else if( MDK.Ajax.xmlHttp.responseText == 'Err[3]' )
						$M.span.innerHTML	=	$M.Error[5];
					else
					{
						$M.span.innerHTML	=	$M.Error[1];
						window.auth['authcode']	=	true;
					}
				}
				else
				{
					$M.span.innerHTML	=	$M.Error[2];
				}
			}

			MDK.Ajax.init( 'post', './u/register.php', reqAuthcode, value );
		}
		else
		{
			this.span.innerHTML	=	this.Error[0] + this.Error[3];
			return	false;
		}
	}
})